package proyecto.testcasetool.presentation.details.scenario;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import proyecto.testcasetool.impl.ParameterImpl;
import proyecto.testcasetool.presentation.details.DetailsPage;
import proyecto.testcasetool.presentation.editor.TestcasetoolEditorPlugin;
import proyecto.testcasetool.presentation.editor.pages.FormLayoutFactory;


public class BasicFlowDetailsPage extends DetailsPage implements ISelectionChangedListener {

	private Table stepsTable;
	private Table parametersTable;
	private TableViewer stepsTableViewer;
	private TableViewer parametersTableViewer;
	
	public BasicFlowDetailsPage(FormToolkit toolkit, EditingDomain editingDomain,
			AdapterFactory adapterFactory) {
		super(toolkit, editingDomain, adapterFactory);
	}
	@Override
	protected void createDetailsSection(Composite parent) {
		Section sectionDetalles = toolkit.createSection(parent,
				ExpandableComposite.EXPANDED | ExpandableComposite.TITLE_BAR);
		sectionDetalles.setText("Sección detalles");
		//
		sectionDetalles.setLayoutData(new GridData(GridData.FILL_BOTH));
		sectionDetalles.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1));
		sectionDetalles.setMenu(new Menu(sectionDetalles));
		Composite compositeDetalles = toolkit.createComposite(sectionDetalles, SWT.NONE);
		
		toolkit.paintBordersFor(compositeDetalles);
		sectionDetalles.setClient(compositeDetalles);

		Section sectionActions = toolkit.createSection(parent,
				ExpandableComposite.EXPANDED | ExpandableComposite.TITLE_BAR);
		sectionActions.setText("Sección de Steps");
		sectionActions.setLayoutData(new GridData(GridData.FILL_BOTH));
		sectionActions.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1));
		
		Composite compositeSteps = toolkit.createComposite(sectionActions, SWT.NONE);
		
		toolkit.paintBordersFor(compositeSteps);
		sectionActions.setClient(compositeSteps);
		compositeSteps.setLayout(new GridLayout(1, false));
		compositeSteps.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		Button button = toolkit.createButton(compositeSteps, "", SWT.PUSH);
	    Object o = TestcasetoolEditorPlugin.getPlugin().getImage("16x16/Add_16x16.gif");
		Image image = ExtendedImageRegistry.INSTANCE.getImage(o);
		button.setImage(image);
		//button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
	    button.addSelectionListener(new SelectionAdapter() {
	      public void widgetSelected(SelectionEvent e) {
//	    	  Action action = EssentialUseCaseFactory.eINSTANCE.createAction();
//	    	  action.setDescription("descripcion de una accion");
//	    	  Command cmd = AddCommand.create(editingDomain, eObject, EssentialUseCasePackage.Literals.FLOW_OF_EVENTS__STEP, action);
//	    	  editingDomain.getCommandStack().execute(cmd);
	    	  
	      }
	    });
	    
		stepsTableViewer = new TableViewer(compositeSteps, SWT.BORDER | SWT.FULL_SELECTION);
		stepsTable = stepsTableViewer.getTable();
		stepsTable.setLinesVisible(true);
		stepsTable.setHeaderVisible(true);
		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd.heightHint = 100;
		stepsTable.setLayoutData(gd);
		toolkit.paintBordersFor(stepsTable);
		
		TableViewerColumn tableViewerColumn = new TableViewerColumn(stepsTableViewer, SWT.NONE);
		TableColumn tblclmnDato = tableViewerColumn.getColumn();
		tblclmnDato.setWidth(100);
		tblclmnDato.setText("Dato1");
		
		tableViewerColumn.setLabelProvider(new ColumnLabelProvider() {
			  @Override
			  public String getText(Object element) {
//				    ActionImpl p = (ActionImpl) element;
//				    return p.getDescription();
				    return "pepe";
			  }
			});
		
		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(stepsTableViewer, SWT.NONE);
		TableColumn tblclmnDato_1 = tableViewerColumn_1.getColumn();
		tblclmnDato_1.setWidth(100);
		tblclmnDato_1.setText("dato2");
		tableViewerColumn_1.setLabelProvider(new ColumnLabelProvider() {
			  @Override
			  public String getText(Object element) {
//			    ActionImpl p = (ActionImpl) element;
//			    return p.getDescription();
			    return "pepe";
			  }
			});
		
		stepsTableViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory));
		stepsTableViewer.addSelectionChangedListener(this);
		
		
		Section sectionParameters = toolkit.createSection(parent,
				ExpandableComposite.EXPANDED | ExpandableComposite.TITLE_BAR);
		sectionParameters.setText("Sección de Parametros");
		sectionParameters.setLayoutData(new GridData(GridData.FILL_BOTH));
		sectionParameters.setLayout(FormLayoutFactory.createDetailsGridLayout(false, 1));
		
		Composite compositeParameters = toolkit.createComposite(sectionParameters, SWT.NONE);
		
		toolkit.paintBordersFor(compositeParameters);
		sectionParameters.setClient(compositeParameters);
		compositeParameters.setLayout(new GridLayout(1, false));
		compositeParameters.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		Button buttonCreateParameter = toolkit.createButton(sectionParameters, "", SWT.PUSH);
		buttonCreateParameter.setImage(image);
	    buttonCreateParameter.addSelectionListener(new SelectionAdapter() {
	      public void widgetSelected(SelectionEvent e) {
//	    	  Parameter parameter = EssentialUseCaseFactory.eINSTANCE.createParameter();
//	    	  parameter.setDescription("descripcion del parametro");
//	    	  parameter.setName("Nombre de parametro");
//	    	  Command cmd = AddCommand.create(editingDomain, getEObjectFromSelection(stepsTableViewer.getSelection()), EssentialUseCasePackage.Literals.ACTION__PARAMETERS, parameter);
//	    	  editingDomain.getCommandStack().execute(cmd);
	      }
	    });
	    sectionParameters.setTextClient(buttonCreateParameter);
		
		// Forma de agregar acciones a la seccion sustituye lo de arriba
//		ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
//		ToolBar toolbar = toolBarManager.createControl(sectionParameters);
//		toolBarManager.add(new CrearParametroAction());
//		toolBarManager.update(true);
//		sectionParameters.setTextClient(toolbar);
	    
		parametersTableViewer = new TableViewer(compositeParameters, SWT.BORDER | SWT.FULL_SELECTION);
		parametersTable = parametersTableViewer.getTable();
		parametersTable.setLinesVisible(true);
		parametersTable.setHeaderVisible(true);
		GridData gd2 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd2.heightHint = 100;
		parametersTable.setLayoutData(gd2);
		toolkit.paintBordersFor(parametersTable);
		
		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(parametersTableViewer, SWT.NONE);
		TableColumn tblclmnDato_2 = tableViewerColumn_2.getColumn();
		tblclmnDato_2.setWidth(100);
		tblclmnDato_2.setText("dato11");
		
		tableViewerColumn_2.setLabelProvider(new ColumnLabelProvider() {
			  @Override
			  public String getText(Object element) {
			    ParameterImpl param = (ParameterImpl) element;
			    return param.getName();
			  }
			});
				
		parametersTableViewer.setContentProvider(new AdapterFactoryContentProvider(adapterFactory));
		
	}
	
	public void selectionChanged(IFormPart part, ISelection selection) {
		// controlar que sea la part que esto esperando!!!!
		if (!(selection instanceof IStructuredSelection)) {
			return;
		}
		EObject newEObject = getEObjectFromSelection(selection);
		if (newEObject != null && newEObject != eObject) {
			eObject = newEObject;
			if (eObject != null) {
				System.out.println("DetailsPage.selectionChanged"+
						"\n"+part+"\n"+selection);
				stepsTableViewer.setInput(eObject);
			}
		}
	}
	@Override
	public void selectionChanged(SelectionChangedEvent event) {
		EObject newEObject = getEObjectFromSelection(event.getSelection());
		parametersTableViewer.setInput(newEObject);
	}
	
	public void dispose(){
		stepsTableViewer.removeSelectionChangedListener(this);
		super.dispose();
	}
}
